<template>
	<div>
		<el-card class="box-card">
			<div class="Serach_b">
				<el-input type="text" autosize placeholder="请输入订单编号" v-model="fy.orderNo"
					@keyup.enter.native="SearchData" style="width: 300px; margin-left: 20px">
				</el-input>
				<el-input type="text" autosize placeholder="请输入用户ID" v-model="fy.uid" @keyup.enter.native="SearchData"
					style="width: 300px; margin-left: 20px">
				</el-input>
				<!-- 	<el-date-picker @change="handleChange" v-model="fy.startTime" type="date" placeholder="选择日期">
				</el-date-picker> -->
				<el-button type="primary" icon="el-icon-search" @click="SearchData()">搜索</el-button>
			</div>
			<el-table :data="OrderList" border :header-cell-style="{ textAlign: 'center' }"
				:row-class-name="tableRowClassName">
				<el-table-column type="expand">
					<template slot-scope="props">
						<el-form label-position="left" inline class="demo-table-expand">
							<el-form-item label="收货人:" style="width: 500px">{{props.row.consignee}}</el-form-item>
							<el-form-item label="所在省，市:" style="width: 500px">
								<span>{{ props.row.provinceCityDistrict }}</span>
							</el-form-item>
							<el-form-item label="详细地址:" style="width: 500px">
								<span>{{ props.row.detail }}</span>
							</el-form-item>
							<el-form-item label="收货手机号:" style="width: 500px">{{props.row.phone}}</el-form-item>
							<el-form-item label="备注:" style="width: 500px">
								<span>{{ props.row.remark }}</span>
							</el-form-item>
						</el-form>
					</template>
				</el-table-column>
				<el-table-column prop="orderNo" label="订单号" />
				<el-table-column prop="logisticsOrderNum" label="物流订单号" />
				<el-table-column prop="shopMember" label="存货编码" />
				<el-table-column prop="storeName" label="商品名称" />
				<el-table-column prop="shoppingCount" label="商品数量" />
				<el-table-column prop="vipPrice" label="商品单价" />
				<el-table-column prop="uid" label="用户ID" />
				<el-table-column prop="amount" label="支付金额" />
				<el-table-column prop="payType" label="支付类型">
					消费券
				</el-table-column>
				<el-table-column prop="createTime" label="创建时间" />
				<el-table-column label="订单状态">
					<template slot-scope="scope">
						<span v-if="scope.row.status == 1">待发货</span>
						<span v-if="scope.row.status == 2">待收货</span>
						<span v-if="scope.row.status == 4">已完成</span>
						<span v-if="scope.row.status == 8">已退款</span>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="140px">
					<template slot-scope="scope">
						<div class="button">
							<el-button v-if=" scope.row.status == 1 " type="warning" size="mini" @click="Fh(scope.row)">
								发货</el-button>
							<el-button type="danger" size="mini" @click="DelClick(scope.row)">删除</el-button>
						</div>
					</template>
				</el-table-column>
			</el-table>
			<el-dialog title="物流订单号" :visible.sync="dialogVisible2" width="30%" :before-close="handleClose">
				<el-input placeholder="请输入物流订单号" v-model="input" clearable>
				</el-input>
				<span slot="footer" class="dialog-footer">
					<el-button @click="dialogVisible2 = false">取消</el-button>
					<el-button type="primary" @click="GLYSH(1)">确 定</el-button>
				</span>
			</el-dialog>
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10, 15]"
				:page-size="fy.size" layout="total, sizes, prev, pager, next, jumper" :total="total">
			</el-pagination>
		</el-card>
	</div>
</template>

<script>
	import * as OrderApi from "@/api/Search";
	import * as syOrderApi from "@/api/Add";
	import {
		timestampToTime
	} from "@/utils/date";
	export default {
		data() {
			return {
				menuKey: false,
				fy: {
					size: 10,
					current: 1,
				},
				total: 0,
				timestampToTime,
				OrderList: [],
				dialogVisible2: false,
				form: {},
				input: "",
				orderNo: "",
				user: "",
			};
		},
		methods: {
			// 表格隔行变色
			tableRowClassName({
				row,
				rowIndex
			}) {
				if (rowIndex % 2 === 1) {
					return "success-row"; //这是类名
				} else {
					return "";
				}
			},
			dateArrayTransfer(dateArray) {
				if (dateArray == null || dateArray == "") {
					return "无";
				}
				if (dateArray.length == 6) {
					var returnDate =
						dateArray[0] +
						"-" +
						this.returnDoubleNum(dateArray[1]) +
						"-" +
						this.returnDoubleNum(dateArray[2]) +
						" " +
						this.returnDoubleNum(dateArray[3]) +
						":" +
						this.returnDoubleNum(dateArray[4]) +
						":" +
						this.returnDoubleNum(dateArray[5]);
				} else {
					var returnDate =
						dateArray[0] +
						"-" +
						this.returnDoubleNum(dateArray[1]) +
						"-" +
						this.returnDoubleNum(dateArray[2]) +
						" " +
						this.returnDoubleNum(dateArray[3]) +
						":" +
						this.returnDoubleNum(dateArray[4]) +
						":00";
				}
				return returnDate;
			},
			returnDoubleNum(number) {
				return (Array(2).join(0) + number).slice(-2); //创建一个长度为2的数组，且默认用0填充；然后用传过来的数添加都右边，然后从右向左截取两位！
			},
			handleChange() {
				this.SearchData();
			},
			GLYSH() {
				syOrderApi
					.crmOrderDeliverGoodsQiCai({
						orderNo: this.orderNo,
						logisticsOrderNum: this.input,
						token: this.$store.getters.token,
					})
					.then((res) => {
						if (res.data == true) {
							this.SearchData();
							this.dialogVisible2 = false;
							this.$notify({
								title: "成功",
								message: "成功",
								type: "success",
							});
							this.dialogVisible3 = false;
							this.input = "";
						} else {
							this.dialogVisible2 = false;
							this.input = "";
							this.$notify({
								title: "失败",
								message: "失败",
								type: "error",
							});
						}
					});
			},
			Fh(row) {
				this.dialogVisible2 = true;
				this.orderNo = row.orderNo;
			},
			DelClick(row) {
				this.$confirm("确认删除？")
					.then((_) => {
						syOrderApi
							.DelOrder({
								idList: row.crmOrderId,
							})
							.then((res) => {
								if (res.data == true) {
									this.SearchData();
									this.$notify({
										title: "成功",
										message: "删除成功",
										type: "success",
									});
								} else {
									this.SearchData();
									this.$notify({
										title: "失败",
										message: "删除失败",
										type: "error",
									});
								}
							});
						done();
					})
					.catch((_) => {});
			},
			handleSizeChange(value) {
				this.fy.size = value;
				this.SearchData();
			},
			handleCurrentChange(value) {
				this.fy.current = value;
				this.SearchData();
			},
			SearchData() {
				let startTime = timestampToTime(this.fy.startTime);
				OrderApi.InsertShopOrder({
					...this.fy,
					startTime: startTime ? startTime : "",
				}).then((res) => {
					this.total = res.data.total;
					this.OrderList = res.data.list;
					this.OrderList.map((item) => {
						item.createTime = this.dateArrayTransfer(item.createTime);
						item.paymentTime = this.dateArrayTransfer(item.paymentTime);
						item.updateTime = this.dateArrayTransfer(item.updateTime);
					});
				});
			},
			handleClose() {
				this.dialogVisible2 = false;
				this.input = "";
			},
		},
		created() {
			this.user = localStorage.getItem("user");
			let roleMenuList = sessionStorage.getItem("roleMenuList");
			roleMenuList = JSON.parse(roleMenuList);
			for (let i = 0; i < roleMenuList.length; i++) {
				if (roleMenuList[i].menuKey == "ddglczqz") {
					this.menuKey = true;
				}
			}
			this.SearchData();
		},
	};
</script>

<style lang="scss" scoped>
	.el-pagination {
		margin-top: 10px;
	}

	.box-card {
		width: 95%;
		margin: auto;
	}

	.el-form-item {
		width: 300px;
	}

	.Serach_b {
		display: flex;
		align-items: center;
		margin-bottom: 20px;
	}

	.Serach_b .el-button {
		margin-left: 20px;
	}

	.el-input,
	.el-select {
		margin-left: 10px;
	}

	.button {
		display: flex;
	}

	.margin {
		margin-bottom: 20px;
	}

	.success-row {
		background-color: #f6f7fb !important;
	}
</style>
